﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Windows.Forms;

namespace NumericalMethod
{
    public partial class MainFrame : Form
    {

        public MainFrame()
        {
            InitializeComponent();
        }

        private void OpenFile(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
            openFileDialog.Filter = "Numerical Method Files (*.nmt)|*.nmt|All Files (*.*)|*.*";
            if (openFileDialog.ShowDialog(this) == DialogResult.OK)
            {
                string FileName = openFileDialog.FileName;
            }
        }

        private void SaveAsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
            saveFileDialog.Filter = "Numerical Method Files (*.nmt)|*.nmt|All Files (*.*)|*.*";
            if (saveFileDialog.ShowDialog(this) == DialogResult.OK)
            {
                string FileName = saveFileDialog.FileName;
            }
        }

        private void ExitToolsStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void CascadeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.Cascade);
        }

        private void TileVerticalToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.TileVertical);
        }

        private void TileHorizontalToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.TileHorizontal);
        }

        private void ArrangeIconsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.ArrangeIcons);
        }

        private void CloseAllToolStripMenuItem_Click(object sender, EventArgs e)
        {
            foreach (Form childForm in MdiChildren)
            {
                childForm.Close();
            }
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            About AboutDialog = new About();
            AboutDialog.MdiParent = this;
            AboutDialog.Show();
        }

        private void newToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            OpenFile(sender, e);
        }

        private void AddToTree()
        {
            Assembly asm = Assembly.GetExecutingAssembly();
            //create a list for the namespaces
            //create a list that will hold all the classes
            //the suplied namespace is executing
            List<string> returnList = new List<string>();
            //loop through all the "Types" in the Assembly using
            Type numericPanel = typeof(ContentPane);
            BindingFlags flags = (BindingFlags.NonPublic |
                                 BindingFlags.Public |
                                 BindingFlags.Static |
                                 BindingFlags.Instance |
                                 BindingFlags.DeclaredOnly);

            TreeNode root = treeView1.Nodes.Add( "Numerical Methods" );
            foreach (Type type in asm.GetTypes())
            {
                if (type.Namespace == "NumericalMethod"
                    && type.BaseType == numericPanel)
                {
                    MethodInfo[] mi = type.GetMethods(flags);
                    TreeNode notesNode;
                    ContentPane obj = (ContentPane)Activator.CreateInstance(type);
                    bool NewNodeFlag = true;

                    foreach(TreeNode nodes in root.Nodes )
                    {
                        if ( nodes.Name == "r"+obj.Chapter )
                        {
                            if (!string.IsNullOrEmpty(obj.ChapterName))
                                root.Nodes[obj.Chapter].Text = obj.ChapterName;

                            notesNode = nodes.Nodes.Add(type.Name, obj.TreeViewText);
                            notesNode.Tag = obj;
                            notesNode = notesNode.Nodes.Add("Notes");
                            notesNode.Tag = obj;     
                            NewNodeFlag = false;
                        }
                    }
                    
                    if( NewNodeFlag )
                    {
                        if (obj.Chapter == -1)
                            continue;
                       TreeNode node = root.Nodes.Add( "r"+obj.Chapter, "_" );
                       
                       if ( !string.IsNullOrEmpty( obj.ChapterName ) )
                          node.Text = obj.ChapterName;
                       notesNode = node.Nodes.Add( type.Name, obj.TreeViewText );
                       notesNode.Tag = obj;
                       // Add note
                       notesNode = notesNode.Nodes.Add("Notes");
                       notesNode.Tag = obj;
                       
                    }
                }
            }
            //promote 1 level if lonely
            foreach (TreeNode nodes in root.Nodes)
            {
                if (nodes.Nodes.Count == 1)
                {
                    root.Nodes[nodes.Index] = nodes.FirstNode;
                    root.Nodes[nodes.Index].Text = ((ContentPane)nodes.FirstNode.Tag).ChapterName;
                }
            }

            root.Expand();
            root.Tag = new Welcome(); 
            treeView1.Sort();
        }

        private void TreeView1_Autofit()
        {
           treeView1.Height = splitContainer1.Panel1.Height;
           treeView1.Width = splitContainer1.Panel1.Width;
        }

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
           contentPanel.Controls.Clear();
           if (null != treeView1.SelectedNode.Tag)
           {
               if (treeView1.SelectedNode.Text == "Notes")
               {
                   ContentPane c = (ContentPane)treeView1.SelectedNode.Tag;
                   Notes n = new Notes(c.Notes);
                   splitContainer1.Panel2.Controls.Clear();
                   splitContainer1.Panel2.Controls.Add( n );
                   n.Dock = DockStyle.Fill;
               }
               else
               {
                  splitContainer1.Panel2.Controls.Clear();
                  splitContainer1.Panel2.Controls.Add( contentPanel );
                   contentPanel.Controls.Add((ContentPane)treeView1.SelectedNode.Tag);
                   ((ContentPane)treeView1.SelectedNode.Tag).OnLoad();
               }
           }
           
           
        }

        private void splitContainer1_Panel2_Paint(object sender, PaintEventArgs e)
        {
            contentPanel.Height = splitContainer1.Panel2.Height;
            contentPanel.Width = splitContainer1.Panel2.Width;
        }

        private void splitContainer1_Panel1_Paint(object sender, PaintEventArgs e)
        {
            TreeView1_Autofit();
        }

        private void MainFrame_Load(object sender, EventArgs e)
        {
            AddToTree();
        }
    }
}
